<!-- Sofija Puric 6/08 -->
<?php
include_once dirname(__FILE__) . '/staticStars.php'; 

$keyword = $_POST["keyword"];
$price_from = $_POST["fprice"];
$price_to = $_POST["tprice"];

    function check_keyword ($keyword) {
        $keyword = trim($keyword);
        if (is_string($keyword))
            return true;
        else return false;
      }
    function check_price ($price)  {
        $price = trim($price);
        if (is_numeric($price) && $price >= 0 )
            return true;
        else return false;
      }
    function check_prices ($price_from, $price_to) {
        if ($price_to >= $price_from)
            return true;
        else return false;
    }
    
    if (!check_keyword($keyword) && isset($keyword) && !empty($keyword)){
      header('Location: hostels.php?err=1');
    }
    if (!check_price($price_from) && isset($price_from) && !empty($price_from)){
      header('Location: hostels.php?err=2');
     }

     if (!check_price($price_to) && isset($price_to) && !empty($price_to)){
      header('Location: hostels.php?err=3');
     }
     if (isset($price_from) && !empty($price_from) && check_price($price_from)
             && isset($price_to) && !empty($price_to) && check_price($price_to)
             && !check_prices($price_from, $price_to) ){
         header('Location: hostels.php?err=4');
             }
     if (empty($keyword) && empty($price_from) && empty($price_to)) {
         header('Location: hostels.php?err=5');
     }

     require 'db.php';
     $con=dbConnect('sib');

     if (!empty($keyword) && empty($price_from) && empty($price_to)) {    // unet samo keyword
         $sql = "SELECT * FROM hostel WHERE LOCATE('$keyword', Name) >= 1";
         $result = mysql_query($sql, $con);
         if (!$result)
               die('Error: ' . mysql_error());
     }
     else if (empty($keyword) && !empty($price_from) && empty($price_to)) {   // unet samo price_from
         $sql = "SELECT * FROM hostel WHERE PriceMin >= '$price_from' OR (PriceMin <= '$price_to' AND PriceMax >= '$price_to')";
         $result = mysql_query($sql, $con);
         if (!$result)
               die('Error: ' . mysql_error());
     }
     else if (empty($keyword) && empty($price_from) && !empty($price_to)) {   // unet samo price_to
         $sql = "SELECT * FROM hostel WHERE PriceMax <= '$price_to' OR (PriceMin <= '$price_to' AND PriceMax >= '$price_to')";
         $result = mysql_query($sql, $con);
         if (!$result)
               die('Error: ' . mysql_error());
     }
     else if (!empty($keyword) && !empty($price_from) && empty($price_to)) {  // uneti keyword i price_from
         $sql = "SELECT * FROM hostel WHERE LOCATE('$keyword', Name) >= 1 AND (PriceMin >= '$price_from' OR (PriceMin <= '$price_to' AND PriceMax >= '$price_to'))";
         $result = mysql_query($sql, $con);
         if (!$result)
               die('Error: ' . mysql_error());
     }
     else if (!empty($keyword) && empty($price_from) && !empty($price_to)) {  // uneti keyword i price_to
         $sql = "SELECT * FROM hostel WHERE LOCATE('$keyword', Name) >= 1 AND (PriceMax <= '$price_to' OR (PriceMin <= '$price_to' AND PriceMax >= '$price_to'))";
         $result = mysql_query($sql, $con);
         if (!$result)
               die('Error: ' . mysql_error());
     }
     else if (empty($keyword) && !empty($price_from) && !empty($price_to)) {   // uneti price_from i price_to
         $sql = "SELECT * FROM hostel WHERE (PriceMin >= '$price_from' OR (PriceMin <= '$price_to' AND PriceMax >= '$price_to')) AND
         (PriceMax <= '$price_to' OR (PriceMin <= '$price_to' AND PriceMax >= '$price_to'))";
         $result = mysql_query($sql, $con);
         if (!$result)
               die('Error: ' . mysql_error());
     }
     else if (!empty($keyword) && !empty($price_from) && !empty($price_to)) {  // uneto sve
         $sql = "SELECT * FROM hostel WHERE (PriceMin >= '$price_from' OR (PriceMin <= '$price_to' AND PriceMax >= '$price_to')) AND
         (PriceMax <= '$price_to' OR (PriceMin <= '$price_to' AND PriceMax >= '$price_to')) AND LOCATE('$keyword', Name) >= 1";
         $result = mysql_query($sql, $con);
         if (!$result)
               die('Error: ' . mysql_error());
     }

     function writeStars($HostelID) {
        $query = "SELECT (avg(Security)+avg(Location)+avg(Service)+avg(Fun)+avg(Cleanliness)+avg(Price))/6 AS prosek " .
                "FROM feedback WHERE IDHostel = $HostelID;";
        $conn = dbConnect("sib");
        $result = mysql_query($query, $conn);
        $row = mysql_fetch_array($result);
        $prosek = $row["prosek"];
        $ss = new staticStars(round($prosek));
        return $ss->printStars($prosek);
    }

     if (isset($result) && !empty($result)) {
          while ( $row = mysql_fetch_array($result) ) {
              $idh=$row["IDHostel"];
        $sql_photo = "SELECT * FROM photo WHERE IDHostel = '$idh' AND IndexPhoto = '1'";
        $result_photo = mysql_query($sql_photo, $con);
        if (!$result_photo)
            die('Error: ' . mysql_error());
        $row_photo = mysql_fetch_array($result_photo);
              echo '<div class="hostel">
		<div class="title"><h2>' . $row["Name"] . '</h2></div>
		<a href = "hostel.php?hid=' . $idh . '"><img src="'.$row_photo["Url"].'" alt="'.$row_photo["Alt"].'"/></a>
                    <div class="rate_choice hostels_stars">';
              echo writeStars($idh);
              echo '  </div>
		<div class="entry">' . $row["Description"] . '</div>';
                $idh=$row["IDHostel"];
                echo '
                <div class="links"><a href="hostel.php?hid='.$idh.'">See more</a></div>

	</div>';

            }
     }
     


    ?>
